/* $$Id: WaitFlash.java 24 2008-01-08 16:19:22Z qxodream $$
 *
 * RssReaderMidlet --  RSS reader for J2ME(midp)
 * The original codes of this porgram is from
 * RssOwlMIDlet(zhengyun@gmail.com)
 *
 * Copyright (c) 2007-2008 QXO(qxodream@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.ultrapower.view;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 * 类说明：“动画等待画面”的Canvas界面 此类可用于长时间操作时的等待画面
 *
 * @author 张文杰
 * @author QXO(qxodream@gmail.com)
 *
 */
public class WaitFlash extends Canvas {

	private int mCount, mMaxinum;

	private int mInterval;

	private int mWidth, mHeight, mx, my, mRadius;

	private String Msg;

	protected void paint(Graphics g) {
		int theta = -(mCount * 360 / mMaxinum);

		// /////////////
		// 以下setColor我们是要定义动画的背景色彩
		// g.setColor(255, 255, 255); // 这是白色
		// g.setColor(8,65,99); // 这是深蓝色
		g.setColor(222, 235, 198); // 这是浅绿色
		g.fillRect(0, 0, mWidth, mHeight);
		// /////////////

		// /////////////
		// 以下setColor我们是要定义动画中圆形物体的边缘色彩
		// g.setColor(0x00ff0000); // 这是红色
		g.setColor(173, 195, 41); // 这是绿色
		// /////////////
		g.drawArc(mx, my, mRadius, mRadius, 0, 360);

		// /////////////
		// 以下setColor我们是要定义动画中圆形物体的填充色彩
		// g.setColor(255,154,49);// 橙色
		g.setColor(115, 166, 49);// 深绿色
		// /////////////
		g.fillArc(mx, my, mRadius, mRadius, theta + 90, 90);
		g.fillArc(mx, my, mRadius, mRadius, theta + 270, 90);

		if (Msg != null) {
			g.drawString(Msg, mWidth / 2, my + mRadius + 20, Graphics.BOTTOM
					| Graphics.HCENTER);
		}
	}

	public WaitFlash() {
		mCount = 0;
		mMaxinum = 36;
		mInterval = 50;
		mWidth = getWidth();
		mHeight = getHeight();

		int halfWidth = (mWidth - mRadius) / 2;
		int halfHeight = (mHeight - mRadius) / 2;
		mRadius = Math.min(halfWidth, halfWidth) - 10;

		mx = halfWidth - mRadius / 2;
		my = halfHeight - mRadius / 2;

		TimerTask task = new TimerTask() {
			public void run() {
				mCount = (mCount + 1) % mMaxinum;
				repaint();
			}
		};

		Timer timer = new Timer();
		timer.schedule(task, 0, mInterval);
	}

	public void setMessage(String messge) {
		Msg = messge;
		repaint();
	}
}
